module.exports = function ($scope,$stateParams, $resource, $state, $http, toaster) {
  var tcode = $stateParams.tcode
  console.log(tcode);
  /**
   * 初始化经度
   */
  $scope.longitude = 123.492522;
  /**
   * 初始化纬度
   */
  $scope.latitude= 41.812736;
/**
       * 初始化百度地图
       */
      
      $scope.initBap = function () {
        $scope.map = new BMap.Map("container"); // 创建地图实例
        $scope.map.centerAndZoom(new BMap.Point($scope.longitude, $scope.latitude), 15);  // 初始化地图，设置中心点坐标和地图级别
        $scope.map.addControl(new BMap.NavigationControl());
        $scope.map.addControl(new BMap.ScaleControl());
        $scope.map.addControl(new BMap.OverviewMapControl());
        $scope.map.enableScrollWheelZoom(true);

        var driving = new BMap.DrivingRoute($scope.map);

        // var p1 = new BMap.Point(123.485482,41.808768);
        // var p2 = new BMap.Point(123.497699,41.81358);
        
        // driving.search(p1, p2);
        driving.setSearchCompleteCallback(function(){
          var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
  
          var polyline = new BMap.Polyline(pts);     
          $scope.map.addOverlay(polyline);
          // $scope.addmarker(p1);
          // $scope.addmarker(p2);
        })




      $scope.addmarker = function (point,name){
          var myIcon = new BMap.Icon("http://shangke2.oss-cn-qingdao.aliyuncs.com/img/map_mark.png", new BMap.Size(21,60));
          var marker = new BMap.Marker(point,{icon:myIcon});
          $scope.map.addOverlay(marker);
          $scope.createTag(marker,name);
        }
      };

      $scope.addmarkergreen = function (point,time){
        var myIcon = new BMap.Icon("http://shangke2.oss-cn-qingdao.aliyuncs.com/img/mark_green.png", new BMap.Size(21,60));
        var marker = new BMap.Marker(point,{icon:myIcon});
        var time = "到达时间为："+time;
        $scope.map.addOverlay(marker);
        $scope.createTag(marker,time);
      }
    
     
      $scope.createTag = function (marker,name){

        //标注
        var text =  name ; 
        //给标记添加点击事件以及显示窗口信息
         var infoWindow = new BMap.InfoWindow( text );
         marker.addEventListener("click", function () { 
           this.openInfoWindow(infoWindow);
           
         });
    }


    $resource('/api/as/ggc/tourgroupafter/findGTList', {}, {}).save({'tourgroup_code':tcode},function (res) {
  
      if (res.errcode !== 0) {
        alert("数据获取失败");
        return;
      }
      var resdata = res.data;
      console.log(res.data.length);
      for(var no in resdata){
        var log = resdata[no].center_log;
        var lat = resdata[no].center_lat;
        var name = resdata[no].center_name;
        var marker = new BMap.Point(log,lat);
        $scope.addmarker(marker,name);
      }
      /*以上是标点    以下是画线*/
      var driving = new BMap.DrivingRoute($scope.map);
      for(i=1;i<res.data.length;i++){
        var p1 = new BMap.Point(res.data[i-1].center_log,res.data[i-1].center_lat);
        var p2 = new BMap.Point(res.data[i].center_log,res.data[i].center_lat);
        
        driving.search(p1, p2);
        driving.setSearchCompleteCallback(function(){
          var pts = driving.getResults().getPlan(0).getRoute(0).getPath();    //通过驾车实例，获得一系列点的数组
  
          var polyline = new BMap.Polyline(pts);     
          $scope.map.addOverlay(polyline);
        })
      }
    });
    
    /*下面为实际的轨迹线 */
    $resource('/api/as/ggc/tourgroupafter/findPracticalList', {}, {}).save({'tourgroup_code':tcode},function (res) {
      if (res.errcode !== 0) {
        alert("数据获取失败");
        return;
      }
      var resdata = res.data;
      for(var no in resdata){
        var log = resdata[no].practical_log;
        var lat = resdata[no].practical_lat;
        var time = resdata[no].practical_time;
        var marker = new BMap.Point(log,lat);
        $scope.addmarkergreen(marker,time);
      }
      /*以上是标点    以下是画线*/
      for(i=1;i<res.data.length;i++){
        var p1 = new BMap.Point(res.data[i-1].practical_log,res.data[i-1].practical_lat);
        var p2 = new BMap.Point(res.data[i].practical_log,res.data[i].practical_lat);
        
        var polyline = new BMap.Polyline([
          p1,//起始点的经纬度
          p2//终止点的经纬度
          ], {strokeColor:"red",//设置颜色 
          strokeWeight:5, //宽度
          strokeOpacity:0.5});//透明度
          $scope.map.addOverlay(polyline);
      }
      $scope.map.panTo(new BMap.Point(resdata[0].practical_log, resdata[0].practical_lat));
    })

      $scope.initBap();
      
  }